/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.to.aboomy.bannersample.indicator.magicindicator;

/**
 * 实现颜色渐变，考虑到兼容性，不使用内置的ArgbEvaluator
 *
 * @since 2020-09-29
 */
public class ArgbEvaluatorHolder {
    private static final int NUM24 = 24;
    private static final int NUMFF = 0XFF;
    private static final int NUM16 = 16;
    private static final int NUM8 = 8;
    private static final int NUM = 8;

    private ArgbEvaluatorHolder() {
    }

    /**
     * eval
     *
     * @param fraction fraction
     * @param startValue startValue
     * @param endValue endValue
     * @return int
     */
    public static int eval(float fraction, int startValue, int endValue) {
        int startA = (startValue >> NUM24) & NUMFF;
        int startR = (startValue >> NUM16) & NUMFF;
        int startG = (startValue >> NUM8) & NUMFF;
        int startB = startValue & NUMFF;

        int endA = (endValue >> NUM24) & NUMFF;
        int endR = (endValue >> NUM16) & NUMFF;
        int endG = (endValue >> NUM8) & NUMFF;
        int endB = endValue & NUMFF;

        int currentA = (startA + (int) (fraction * (endA - startA))) << NUM24;
        int currentR = (startR + (int) (fraction * (endR - startR))) << NUM16;
        int currentG = (startG + (int) (fraction * (endG - startG))) << NUM8;
        int currentB = startB + (int) (fraction * (endB - startB));

        return currentA | currentR | currentG | currentB;
    }
}
